home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / itrns211.zip / SRC / UTILS.C < prev    next >
C/C++ Source or Header  |  1991-10-14  |  8KB  |  265 lines

  1. /*
  2.  *========================================================================== 
  3.  * Copyright 1991 Avinash Chopde, All Rights Reserved.
  4.  *
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of Avinash Chopde not be used in
  10.  * advertising or publicity pertaining to distribution of the software
  11.  * without specific, written prior permission.
  12.  * Avinash Chopde makes no representations about the suitability of this
  13.  * software for any purpose.
  14.  * It is provided "as is" without express or implied warranty.
  15.  *
  16.  * AVINASH CHOPDE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  18.  * IN NO EVENT SHALL AVINASH CHOPDE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  20.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  21.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  *
  24.  * Author:  Avinash Chopde, 1991
  25.  *        C2 Colonial Drive #4, Andover, MA 01810, USA.
  26.  *
  27.  */
  28.  
  29. static char S_RCSID[] = "$Header: e:/itrans/src/rcs/utils.c 1.5 91/10/14 00:40:18 avinash Exp $";
  30.  
  31. #include "itrans.h"
  32.  
  33. /* =================================================================== */
  34.  
  35. /*
  36.  *   The search routine takes a directory list, separated by :, and
  37.  *   tries to open a file.  Null directory components indicate current
  38.  *   directory.
  39.  *   Returns the open file descriptor if ok, else NULL.
  40.  */
  41.  
  42. FILE* search_fopen(char* path, char* file, char* mode)
  43. {
  44.    register char *nam ;                 /* index into fname */
  45.    register FILE *fd ;                  /* file desc of file */
  46.    char fname[PATH_MAX] ;             /* to store file name */
  47.    static char *home = 0 ;              /* home is where the heart is */
  48.    int slen;
  49.  
  50.    slen = strlen(file);
  51.    if (slen < 1) return NULL;
  52.  
  53.             /* check for full path name */
  54.    if (   (file[0] == DIRSEP)
  55.        || (slen > 1 && file[0] == '.' && file[1] == DIRSEP)
  56.        || (slen > 2 && file[0] == '.' && file[1] == '.' && file[2] == DIRSEP)) {
  57.  
  58.       if ((fd=fopen(file,mode)) != NULL)
  59.          return(fd) ;
  60.       else
  61.          return(NULL) ;
  62.    }
  63.  
  64. #ifdef MSDOS
  65.    if ( isalpha(file[0]) && file[1]==':' ) {   /* if full path name */
  66.       if ((fd=fopen(file,mode)) != NULL)
  67.          return(fd) ;
  68.       else
  69.          return(NULL) ;
  70.    }
  71. #endif
  72.  
  73.    do {
  74.       /* copy the current directory into fname */
  75.       nam = fname;
  76.       /* copy till : */
  77.       if (*path == '~') {
  78.          char *p = nam ;
  79.          path++ ;
  80.          while (*path && *path != PATHSEP && *path != DIRSEP)
  81.             *p++ = *path++ ;
  82.          *p = 0 ;
  83.          if (*nam == 0) {
  84.             if (home == 0) {
  85.                if (home = getenv("HOME")) {
  86.           /*EMPTY*/;
  87.                } else
  88.                   home = "." ;
  89.             }
  90.             strcpy(fname, home) ;
  91.          } else {
  92. #ifdef MSDOS
  93.             /* error("! ~username in path???") ; */
  94.             fprintf(stderr, "! ~username in path???") ;
  95. #else
  96.             struct passwd *pw = getpwnam(fname) ;
  97.             if (pw)
  98.                strcpy(fname, pw->pw_dir) ;
  99.             else {
  100.            fprintf(stderr, "No such user!\n");
  101.         }
  102. #endif
  103.          }
  104.          nam = fname + strlen(fname) ;
  105.       }
  106.       while (*path != PATHSEP && *path) *nam++ = *path++;
  107.       *nam = 0 ;
  108.  
  109.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  110.  
  111.       if (*file != '\0') {
  112.          *nam++ = DIRSEP;                  /* add separator */
  113.          (void)strcpy(nam,file);                   /* tack the file on */
  114.       }
  115.       else
  116.          *nam = '\0' ;
  117.  
  118.       /* belated check -- bah! */
  119.       if ((nam - fname) + strlen(file) + 1 > PATH_MAX) {
  120.          fprintf(stderr, "! overran allocated storage in search_fopen()");
  121.      exit(1);
  122.       }
  123.  
  124. #ifdef DEBUG
  125.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  126. #endif
  127.       if ((fd=fopen(fname,mode)) != NULL)
  128.          return(fd);
  129.  
  130.    /* skip over PATHSEP and try again */
  131.    } while (*(path++));
  132.  
  133.    return(NULL);
  134.  
  135. }               /* end search */
  136.  
  137. /* =================================================================== */
  138. /* compare extensions, "." must be first char of right_ext[].. */
  139. int chk_ext(char *path, char *right_ext)
  140. {
  141.   char *p;
  142.   int err = 0;
  143.  
  144.   /* Find the last '.' in the path */
  145.   p = strrchr(path, '.');
  146.  
  147.   if(!p) {
  148.     err++; /* extension missing.... */
  149.   } else if(strcmp(p, right_ext)) {
  150.     err++; /* incorrect extension */
  151.   }
  152.  
  153.   return(err);
  154. }
  155.  
  156. /* =================================================================== */
  157.  
  158. #ifdef NEED_IDIV1000_A
  159. /* divide the given integer by 1000, convert the result into a string */
  160. /* was written since on the PC, using DJG's GNU compiler, no floating
  161.  * point arith was possible.
  162.  * And, anyway, this may even be faster than float/1000 and call to
  163.  * print...
  164.  * [abc] 5 sept 1991, don't need this anymore, got the DJG GCC compiler
  165.  * working -- instead of saying EMU in the GO32 env var, I had to
  166.  * say emu! (small instead of caps!)
  167.  */
  168. int idiv1000_a(int n, char str[])
  169. {
  170.     int i, j;
  171.  
  172.     i = n / 1000; /* get leading digits */
  173.     n = n - i * 1000; /* this left over, guaranteed < 1000 */
  174.  
  175.     /* itoa(i, str, 10); */
  176.     sprintf(str, "%d", i);
  177.     
  178.     j = i = strlen(str);
  179.  
  180.     str[i++] = '.';
  181.     /* add leading zeros */
  182.     if (n < 10) { /* single digit */
  183.         str[i++] = '0';
  184.         str[i++] = '0';
  185.     } else if (n < 100) { /* two digits */
  186.         str[i++] = '0';
  187.     }
  188.  
  189.     /* itoa(n, &str[i], 10); */
  190.     sprintf(&str[i], "%d", n);
  191.  
  192.     return (j + 4);
  193. }
  194. #endif /*NEED_IDIV1000_A*/
  195. /* =================================================================== */
  196. #ifdef MSDOS
  197. /* support functions that may be missing on a PC/MSDOS system.
  198.  
  199.  * getopt()
  200.    Only simple versions supported:
  201.    options may not be combined (as in the real getopt()), but there may
  202.    not be a space between the arg letter and the option argument.
  203.  */
  204.  
  205. /* externally visible */
  206. char* optarg =0;    int optind =1;    int opterr =1;
  207.  
  208. int getopt(int argc, char* argv[], char* optstring)
  209. {
  210.     int i, j, found;
  211.     char c, *curr;
  212.     char tmp[256];
  213.  
  214.     found = FALSE;
  215.     optarg = NULL;
  216.  
  217.     while (!found) {
  218.  
  219.     curr = argv[optind];
  220.  
  221.         if (!curr || optind >= argc) return -1; /* end of arguments */
  222.  
  223.         if (curr[0] != '-') {
  224.         /* end of arguments */
  225.         return -1;
  226.         }
  227.  
  228.         if (curr[0] == '-' && curr[1] == '-') {
  229.         /* end of arguments */
  230.         return -1;
  231.         }
  232.  
  233.         /* got an argument */
  234.         j = strlen(optstring);
  235.         c = curr[1];
  236.         for (i = 0; i < j; i ++) {
  237.         if (optstring[i] == ':') continue;
  238.     
  239.         if (c == optstring[i]) {
  240.             /* check if needs arguments */
  241.             if (optstring[i+1] == ':') {
  242.             strcpy(tmp, argv[optind]);
  243.             optarg = strtok(tmp+2, "     "); /* space, TAB chars */
  244.             if (!optarg) { /* arg for option is at next location */
  245.                 optind++;
  246.                 optarg = argv[optind];
  247.             }
  248.             }
  249.             found = TRUE;
  250.             break;
  251.         }
  252.         }
  253.  
  254.     if (!found && opterr)  {
  255.         fprintf(stderr, "Unrecognized option: %c -- ignoring it\n",c);
  256.     }
  257.     
  258.         optind++;
  259.     }
  260.  
  261.     return c;
  262. }
  263. #endif
  264. /* =================================================================== */
  265.